package com.yogee.topseller.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.AnimalUtil;
import com.yogee.topseller.R;
import com.yogee.topseller.adapter.IntelligentScreeningBigRvAdapter;
import com.yogee.topseller.adapter.IntelligentScreeningListRvAdapter;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.fragment.product.model.bean.ProductListBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.layout.TitleLayout;
import com.yogee.topseller.utils.RefreshUtil;
import com.yogee.topseller.view.MySinaRefreshView;
import com.yogee.topseller.view.RefreshView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/8/8.
 */

public class PushProductListActivity extends HttpActivity implements RefreshUtil.OnRefreshListener {

    @BindView(R.id.push_product_title_layout)
    TitleLayout titleLayout;
    @BindView(R.id.big_tv)
    TextView bigTv;
    @BindView(R.id.list_tv)
    TextView listTv;
    @BindView(R.id.push_product_rv)
    RecyclerView rv;
    @BindView(R.id.push_product_refreshLayout)
    TwinklingRefreshLayout refreshLayout;

    private IntelligentScreeningBigRvAdapter bigAdapter;//大图适配器
    private IntelligentScreeningListRvAdapter listAdapter;//列表适配器
    private List<InterlligentFilterBean.ProductListBean> bean = new ArrayList<>();
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_push_product;
    }

    @Override
    protected void initView() {
        titleLayout.setTitle("推送列表");
        titleLayout.setActivity(this);

        if (getIntent() != null)
            id = getIntent().getStringExtra("id");

        MySinaRefreshView headerView = new MySinaRefreshView(this);
        refreshLayout.setOnRefreshListener(new RefreshUtil(this).refreshListenerAdapter());
        refreshLayout.setHeaderView(headerView);
        refreshLayout.setBottomView(new RefreshView(this));
        refreshLayout.setBottomHeight(50);
        bigAdapter = new IntelligentScreeningBigRvAdapter(this);
        listAdapter = new IntelligentScreeningListRvAdapter(this);
        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.setHasFixedSize(true);
        rv.setAdapter(bigAdapter);

        getProductList("0", "10", id, "1");
    }

    @OnClick({R.id.big_tv, R.id.list_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.big_tv:
                AnimalUtil.QTanEndAnimal(bigTv, 500);
                bigTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.corens_100_style_s));
                listTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.corens_100_style_white_bg));
                bigTv.setTextColor(Color.parseColor("#ffffff"));
                listTv.setTextColor(getResources().getColor(R.color.colorStyle));
                rv.setAdapter(bigAdapter);
                bigAdapter.addData(bean);
                break;
            case R.id.list_tv:
                AnimalUtil.QTanEndAnimal(listTv, 500);
                bigTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.corens_100_style_white_bg));
                listTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.corens_100_style_s));
                bigTv.setTextColor(getResources().getColor(R.color.colorStyle));
                listTv.setTextColor(Color.parseColor("#ffffff"));
                rv.setAdapter(listAdapter);
                listAdapter.addData(bean);
                break;
        }
    }

    @Override
    public void onRefresh() {
        getProductList("0", "10", id, "1");
    }

    @Override
    public void onLoad() {
        getProductList(bean.size() + "", "10", id, "2");
    }

    private void getProductList(String count, String total, String proId, final String isRefresh) {
        HttpManager.getInstance().getProductList(count, total, proId).compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<InterlligentFilterBean>() {
            @Override
            public void onNext(InterlligentFilterBean o) {
                if (isRefresh.equals("1")) {
                    bean = o.getProductList();
                    refreshLayout.finishRefreshing();
                } else {
                    bean.addAll(o.getProductList());
                    refreshLayout.finishLoadmore();
                }
                bigAdapter.addData(bean);
                listAdapter.addData(bean);
            }
        }, this));
    }
}